/* SVN FILE: $Id: date.js 11 2008-06-22 17:15:42Z rafaelbandeira3 $ */
/**
* Application : The Javascript MVC Framework
*
* @package application.core
* @subpackage cookie
* @author 			$Author: rafaelbandeira3 $
* @copyright 		(c) Rafael Bandeira, Marcelo Condotta, Henrique Condotta. All Rights Reserved.
* @version 			$Revision: 11 $
* @lastrevision 	$Date: 2008-06-22 17:15:42 +0000 (Sun, 22 Jun 2008) $
* @modifiedby 		$LastChangedBy: rafaelbandeira3 $
* @lastmodified 	$LastChangedDate: 2008-06-22 17:15:42 +0000 (Sun, 22 Jun 2008) $
* @license 			The MIT License [http://www.opensource.org/licenses/mit-license.php]
* @filesource 		$URL: http://jsmvcapplication.googlecode.com/svn/trunk/application/src/core/date.js $
*/
App.Date = new Object
({
	monthnames: ['January','February','March','April','May','June','July','August','September','October','November','December'],
	daynames: ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'],
	succ: function()
	{
	    var sd = new Date(this.getFullYear(),this.getMonth(),this.getDate()+1);
	    sd.setHours(this.getHours(),this.getMinutes(),this.getSeconds(),this.getMilliseconds());
	    return sd;
	},
	firstOfMonth: function()
	{
		return new Date(this.getFullYear(),this.getMonth(),1);
	},
	lastOfMonth: function()
	{
		return new Date(this.getFullYear(),this.getMonth()+1,0);
	},
	formatPadding: true,
	format: function(f)
	{
		if (!this.valueOf()) { return '&nbsp;'; }
		var d = this;
		var formats = {
			'yyyy' : d.getFullYear(),
			'yy' : d.getFullYear().toString().substr(2),
			'mmmm': this.monthnames[d.getMonth()],
			'mmm':  this.monthnames[d.getMonth()].substr(0, 3),
			'mm':   this.formatPadding ? ((d.getMonth()).succ()).toPaddedString(2) : (d.getMonth()).succ(),
			'dddd': this.daynames[d.getDay()],
			'ddd':  this.daynames[d.getDay()].substr(0, 3),
			'dd':   d.getDate().toPaddedString(2),
			'hh':   h = d.getHours() % 12 ? h : 12,
			'nn':   d.getMinutes(),
			'ss':   d.getSeconds(),
			'a/p':  d.getHours() < 12 ? 'a' : 'p'
	    };
	    return f.gsub(/(yyyy|mmmm|mmm|mm|dddd|ddd|dd|hh|nn|ss|a\/p)/i,
			function(match) { return formats[match[0].toLowerCase()]; });
	},
	funcs : $w('milliseconds seconds minutes hours date month full-year'),
	funcsAliases : 
	{
		'milliseconds'	: $w('milli millis'),
		'seconds'		: $w('sec secs'),
		'minutes'		: $w('min mins'),
		'hours'			: $w('h hour'),
		'date'			: $w('d day days'),
		'month'			: $w('m months'),
		'full-year'			: $w('y year years')
	},
	clone : function()
	{
		date = new Date();
		funcs = this.funcs.length;
		for(i=0;i<funcs;i++)
		{
			method = this.funcs[i].capitalize().camelize();

			get = 'get'+method;
			set = 'set'+method;
			
			date[set](this[get]());
		}
		return date;
	},
	sum : function(param)
	{
		date = this; 
		if(typeof param == 'string')
		{
			if(['1 d','1 day','1 days'].include(param))
				return this.succ();

			func = false;
			info = param.split(' ');
			sum = Number(info[0]);
			measure = info[1].toLowerCase();

			if(!this.funcs.include(measure))
			{
				for(i in this.funcsAliases)
				{
					aliases = this.funcsAliases[i];
					if(aliases.include(measure))
					{
						func = i;
					}
				}
			}
			else
				func = measure;

			if(func)
			{
				method = func.capitalize().camelize();
				set = 'set'+method;
				get = 'get'+method;

				date[set](date[get]()+sum);
			}
		}
		else if(param instanceof Date)
		{
			/* Does it really make sense? [>] 
			
			iterations = funcs.length;
			for(i=0;i<iterations;i++)
			{
				method = funcs[i].capitalize();
				set = 'set'+method;
				get = 'get'+method;
				
				date[set]();
			}*/
			date = param;
		}
		else if(typeof param == 'number')
		{
			today = date.getDate();			
			date.setDate(today+param)
		}

		return date;
	}
});
Object.extend(Date.prototype, App.Date);
